import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    theme: "",
    messageIsShow: false,
    showRegistration: false,
    departments: [],

    profile: null,
    userInfo: null,
    doctorInfo: null,
  },
  mutations: {
    saveTheme(state, theme) {
      state.theme = theme;
    },
    openMessage(state) {
      state.messageIsShow = true;
    },
    closeMessage(state) {
      state.messageIsShow = false;
    },
    openRegistration(state) {
      state.showRegistration = true;
    },
    closeRegistration(state) {
      state.showRegistration = false;
    },
    setProfile(state, profile) {
      state.profile = profile;
    },
    setDepartments(state, departments) {
      state.departments = departments;
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo;
    },
    setDoctorInfo(state, doctorInfo) {
      state.doctorInfo = doctorInfo;
    },
  },
  actions: {
    async getDepartments(context) {
      const { data } = await axios.get("department/getAllDepartment");
      if (data && data instanceof Array && data.length) {
        context.commit("setDepartments", data);
      }
    },

    async getUserInfo(context, userId) {
      const { data } = await axios.get(`users/getUserInfo?userId=${userId}`);
      if (data) {
        context.commit("setUserInfo", data);
        return data;

      }
    },
    async getDoctorInfo(context, doctorId) {
      const { data } = await axios.get(
        `doctors/getDoctorInfo?doctorId=${doctorId}`
      );
      if (data) {
        context.commit("setDoctorInfo", data);
        return data;
      }
    },
  },
  modules: {},
  getters: {
    isLogin(state) {
      return !!state.profile;
    },
  },
});
